<?php
if(isset($_POST['install'])) :
set_time_limit(600);
$hostname = $_POST['hostname'];
$username = $_POST['username'];
$password = $_POST['password'];
$dbname   = $_POST['database'];

$conn = mysql_connect($hostname, $username, $password);
if(!$conn) {
	echo '<br/>Error connecting database.<br/>' . mysql_error();
	exit;
} else {
    mysql_set_charset('utf8', $conn);
    mysql_query('SET character_set_results = \'utf8\', character_set_client = \'utf8\', character_set_connection = \'utf8\', character_set_database = \'utf8\', character_set_server = \'utf8\'');
}
function dbImportSql($sqlFile) {
    $r = true; //fk
    if(file_exists($sqlFile)) {
        $f = file_get_contents($sqlFile);
        $f = preg_replace('/--\s.+/', '', $f);

        $sqls = explode(';', $f);
        foreach($sqls as $sql) {
            mysql_query($sql);
        }
    }
    return $r;
}
mysql_select_db($dbname);

echo 'Importing dummy .. ', '<br/>';
echo dbImportSql('../docs/db/db-demo.sql') ? '[v]' : '[x]';
echo '<br/>';

echo '<br/>';
echo 'Done.';

else:
?>
<form method="post">
    <hr/>
    <h3 style="text-align:center"> Are you sure want to <input type="submit" name="install" value="import dummy data"  onclick="this.value='Dumping in progress.. '"/> ? </h3>
   <hr/>

    <div>
        <label>Hostname<input type="text" name="hostname" value="localhost" /></label>
        <label>Username<input type="text" name="username" value="root" /></label>
        <label>Password<input type="text" name="password" value="" /></label>
        <label>Database<input type="text" name="database" value="vct" /></label>
    </div>

</form>
<?php endif ?>
